package com.woniu.user.service.Imp;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.woniu.common.CustomException.UserException;
import com.woniu.common.pojo.Shops;
import com.woniu.common.pojo.User;
import com.woniu.common.vo.UserDto;
import com.woniu.common.vo.Users;
import com.woniu.user.mapper.UserMapper;
import com.woniu.user.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class UserServiceImp implements UserService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public UserDto logins(Users user) {
        String src = (String) redisTemplate.opsForValue().get(user.getUuid());
        boolean b = src.equals(user.getCode()) ? true : false;
        if(!b){
            throw new UserException("验证码不正确");
        }
        User tUser = new User();
        tUser.setUsername(user.getUsername());
        List<User> userlist = userMapper.selectOneUser(tUser);
        if(CollectionUtils.isEmpty(userlist)){
            throw new UserException("用户名不存在");
        }
        User user1 = userlist.get(0);
        if(!user.getPassword().equals(user1.getPassword())){
            throw new UserException("用户名或者密码错误");
        }

        Map<String, Object> map = BeanUtil.beanToMap(user1);
        //创建token   map为我们的payload   tUser1.getUsername().getBytes()我们的密钥
        String token = JWTUtil.createToken(map, user1.getUsername().getBytes());
        //redis当中也存入一份token
        redisTemplate.opsForValue().set("999"+user1.getUsername(),token,12, TimeUnit.HOURS);
        UserDto userDto = new UserDto();
        BeanUtil.copyProperties(user1, userDto);
        userDto.setToken(token);
        return userDto;
    }

    @Override
    public void insertUser(User user) {
        if(user == null || user.equals("")){
            throw new UserException("输入不合法");
        }
        User tUser = new User();
        tUser.setUsername(user.getUsername());
        List<User> userlist = userMapper.selectOneUser(tUser);
        if(!CollectionUtils.isEmpty(userlist)){
            throw new UserException("用户名重复了");
        }
        userMapper.addUser(user);
    }

    @Override
    public int selectUserName(String username) {
        try {
            User shops = userMapper.selectUserName(username);
            if (shops != null && shops.getUsername().equals(username)) {
                return 1; // 表示名称已存在
            }
        } catch (Exception e) {
            System.out.println("没有名字叫：" + username);
        }
        return 0; // 表示名称不存在
    }

    @Override
    public User selectUser(User user) {
        return userMapper.selectOne(user);
    }

    @Override
    public void update(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void delete(Integer id) {
        userMapper.deletes(id);
    }
}
